package com.swz.dcrm.ui.mine;


import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.swz.dcrm.adpter.GlobalAdapter;
import com.swz.dcrm.model.BaseResponse;
import com.swz.dcrm.model.Group;
import com.swz.dcrm.ui.base.BaseFragment;
import com.swz.dcrm.R;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.navigation.fragment.NavHostFragment;

import android.widget.TextView;

import butterknife.BindView;
import butterknife.OnClick;

import com.swz.dcrm.R;
import com.swz.dcrm.ui.viewmodel.GroupViewModel;
import com.swz.dcrm.ui.viewmodel.MainViewModel;

import javax.inject.Inject;

/**
 * A simple {@link Fragment} subclass.
 */
public class EditGroupFragment extends BaseFragment {

    @BindView(R.id.toolbar_title)
    TextView title;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.tv_group_name)
    TextView tvGroupName;
    @BindView(R.id.tv_del)
    TextView tvDel;


    @Inject
    GroupViewModel groupViewModel;
    @Inject
    MainViewModel mainViewModel;

    private Group group;

    public static EditGroupFragment newInstance() {

        return new EditGroupFragment();
        // Required empty public constructor
    }

    @Override
    public void onLoadRetry() {
        if (!isNetworkConnected(getContext())){
            mHolder.showLoadingStatus(GlobalAdapter.NET_ERROR);
            return;
        }
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);


        onLoadRetry();
    }

    @Override
    public int layoutId() {
        return R.layout.fragment_edit_group;
    }

    @Override
    public boolean initView() {
        getDigger().inject(this);
        tvRight.setText(getString(R.string.group_edit_save));

        mainViewModel.getEdit().observe(this,edit -> {
            if(edit==null){
                return;
            }
            if(edit.getTitle().equals(getString(R.string.group_edit_name))){
                tvGroupName.setText(edit.getValue());
            }
        });
        EditGroupFragmentArgs groupFragmentArgs=EditGroupFragmentArgs.fromBundle(getArguments());
        if(!groupFragmentArgs.getGroup().equals("test")){
            title.setText(getString(R.string.group_edit_title));
            group=new Gson().fromJson(groupFragmentArgs.getGroup(),Group.class);
            tvGroupName.setText(group.getName());
            tvDel.setVisibility(View.VISIBLE);
        }else {
            title.setText(getString(R.string.group_edit_title1));
            tvDel.setVisibility(View.GONE);
        }

        return true;
    }


    @OnClick(R.id.iv_back)
    public void backClick() {
        NavHostFragment.findNavController(this).navigateUp();
    }
    @OnClick(R.id.ll_group_name)
    public void edit(){


        EditInfoFragmentArgs.Builder builder = new EditInfoFragmentArgs.Builder();

        builder.setTitle(getString(R.string.group_edit_name))
                .setValue(tvGroupName.getText().toString());
        go(null, R.id.action_editGroupFragment_to_editInfoFragment, builder.build().toBundle());
    }
    @OnClick(R.id.tv_right)
    public void save(){
        String name=tvGroupName.getText().toString();
        if(TextUtils.isEmpty(name)){
            showToast("组名不能为空");
            return;
        }
        if(group!=null){
            groupViewModel.updateGroup(group.getId(),name)
                    .observe(this, groupBaseResponse -> {
                        if(groupBaseResponse.isSuccess()){
                            group.setName(name);
                            groupViewModel.getEditGroupResult().setValue(group);
                        }
                        showToast(groupBaseResponse.getMsg());
                    });
        }else {
            groupViewModel.addGroup(name)
                    .observe(this, groupBaseResponse -> {
                        showToast(groupBaseResponse.getMsg());
                        backClick();
                    });
        }
    }

    @Override
    public void onDestroyView() {
        mainViewModel.getEdit().setValue(null);
        super.onDestroyView();
    }

    @OnClick(R.id.tv_del)
    public void del(){
        groupViewModel.delGroup(group.getId())
                .observe(this, groupBaseResponse -> {
                    showToast(groupBaseResponse.getMsg());
                    backClick();
                });
    }
}
